package com.woniuxy.microsystemserver.timer;

import com.woniuxy.bean.SysRateBean;
import com.woniuxy.constant.SysRateConst;
import com.woniuxy.microsystemserver.service.SysRateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Description: 系统费率定时类
 * @Auther: HanLei
 * @Date: 2021/4/15:15:20
 */
@Component
@Slf4j
public class SysRateTimer {

    @Resource(name = "redisTemplate")
    ValueOperations<String, Object> valueOperations;

    @Resource
    private SysRateService sysRateServiceImpl;

    /*
     * Description :  定时更新系统费率(每天0点)
     * ChangeLog : 1. 创建 (2021/4/15 16:19 );
     * @param null
     * @return null
     */
    @Scheduled(cron = "0 0 0 * * ?")//每天23:59:59点执行
    public void updateSysRate1() {
        List<SysRateBean> allRate = sysRateServiceImpl.getAllRate();
        if (allRate != null) {
            //将新费率存入Redis以供其他模块使用
            HashMap<String, SysRateBean> sysRates = new HashMap<>();
            for (SysRateBean rate :
                    allRate) {
                sysRates.put(rate.getRateType(), rate);
            }
            valueOperations.set(SysRateConst.RedisKey.SYSTEM_RATES, sysRates, 24L, TimeUnit.HOURS);
        }
    }


}
